package cn.wjx.taobaounion.ui.activity;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import butterknife.BindView;
import cn.wjx.taobaounion.R;
import cn.wjx.taobaounion.base.BaseActivity;
import cn.wjx.taobaounion.base.BaseFragment;
import cn.wjx.taobaounion.ui.fragment.HomeFragment;
import cn.wjx.taobaounion.ui.fragment.OnSaleFragment;
import cn.wjx.taobaounion.ui.fragment.SearchFragment;
import cn.wjx.taobaounion.ui.fragment.SelectedFragment;
import cn.wjx.taobaounion.utils.LogUtils;

public class MainActivity extends BaseActivity implements HomeFragment.OnHomeSearchClickListener {

    @BindView(R.id.bottom_navigation_view)
    public BottomNavigationView mBottomNavigationView;
    private BaseFragment mLastFragment;
    private HomeFragment mHomeFragment;
    private SelectedFragment mSelectedFragment;
    private OnSaleFragment mOnSaleFragment;
    private SearchFragment mSearchFragment;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        initFragment();
    }

    @Override
    protected void initEvent() {
        initListener();
    }

    private void initFragment() {
        mHomeFragment = new HomeFragment();
        mSelectedFragment = new SelectedFragment();
        mOnSaleFragment = new OnSaleFragment();
        mSearchFragment = new SearchFragment();
        switchFragment(mHomeFragment);
    }

    private void initListener() {
        mBottomNavigationView.setOnNavigationItemSelectedListener(item -> {
            if (item.getItemId() == R.id.home) {
                switchFragment(mHomeFragment);
                LogUtils.d(MainActivity.this, "onNavigationItemSelected: " + item.getTitle());
            } else if (item.getItemId() == R.id.selected) {
                switchFragment(mSelectedFragment);
                LogUtils.d(MainActivity.this, "onNavigationItemSelected: " + item.getTitle());
            } else if (item.getItemId() == R.id.red_package) {
                switchFragment(mOnSaleFragment);
                LogUtils.d(MainActivity.this, "onNavigationItemSelected: " + item.getTitle());
            } else if (item.getItemId() == R.id.search) {
                switchFragment(mSearchFragment);
                LogUtils.d(MainActivity.this, "onNavigationItemSelected: " + item.getTitle());
            }
            return true;
        });
    }

    private void switchFragment(BaseFragment targetFragment) {
        if (mLastFragment == targetFragment)
            return;
        // 修改fragment切换方式，replace=remove+add会销毁视图,转变为add hide show方式
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        if (!targetFragment.isAdded()) {
            transaction.add(R.id.main_page_container, targetFragment);
        }
        if (mLastFragment != null) {
            transaction.hide(mLastFragment);
        }
        mLastFragment = targetFragment;
        transaction.show(targetFragment);
        transaction.commit();
    }

    /**
     * 首页输入框
     * 跳转到搜索界面
     *
     */
    private void switch2SearchFragment() {
        switchFragment(mSelectedFragment);
        mBottomNavigationView.setSelectedItemId(R.id.search);
    }

    @Override
    public void onHomeSearchClick() {
        LogUtils.d(this, "onHomeSearchClick...");
        switch2SearchFragment();
    }
}
